<!-- 
  @Description: 分配工单对话框组件
 -->
<template>
  <el-dialog v-model="dialogVisible" title="分配工单" width="400" draggable @closed="handlerClosed">
    <el-form ref="formRef" style="width: 100%" :model="formData" :rules="formRules" label-width="90px">
      <UserInput itemLabel="用户：" v-model="users" label-width="90px" />
      <el-form-item>
        <div style="display: flex; justify-content: flex-end; width: 100%">
          <el-button type="primary" @click="handlerSubmit()">分配</el-button>
        </div>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { FormInstance } from 'element-plus'
import UserInput from '../../layout/FormTableLayout/UserInput.vue'
import _ from 'lodash'
import { useUsersStore } from '../../stores'
import * as WorkOrder from '../../api/WorkOrder'
import { ElMessage } from 'element-plus'

const emits = defineEmits(['submited'])

const users = ref('') // 用户ID
const workOrderListData = ref<any[]>([]) // 工单列表

const usersStore = useUsersStore()
const { userId, username } = usersStore.getAllState()

// 表单引用
const formRef = ref<FormInstance>()

// 对话框是否显示
const dialogVisible = ref(false)

// 表单数据
const formData = ref({
  id: null as number | null,
  name: null as string | null,
  woCategory: null as string | null,
  userId: null as number | null,
  userName: null as string | null,
  woStatus: null as string | null,
  woPriority: null as string | null,
  woSolution: null as string | null,
  woUserId: null as number | null,
  woUserName: null as string | null,
  description: undefined as string | undefined, // 修改这一行
  imageUrls: [] as string[], // 用于保存上传图片的URL列表
})
// 表单验证规则
const formRules = ref({
  name: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  woCategory: [{ required: true, message: '请选择类型', trigger: 'change' }],
  woStatus: [{ required: true, message: '请选择状态', trigger: 'change' }],
  woPriority: [{ required: true, message: '请选择优先级', trigger: 'change' }],
})

/**
 * 表单提交处理函数
 *
 * @description 该函数用于处理表单提交事件，通过调用WorkOrder.add函数将表单数据提交到服务器，并返回结果
 * @returns {Promise<void>} - 返回一个Promise对象，表示异步操作的完成状态
 */
const handlerSubmit = async () => {
  const res: Promise<any>[] = []

  workOrderListData.value.forEach((item) => {
    item.woUserId = userId
    item.woUserName = username
    res.push(WorkOrder.put(item))
  })

  Promise.all(res)
    .then(() => {
      console.log('分配成功', res)
      ElMessage.success(`分配成功，共分配${workOrderListData.value.length}个工单！`)
      handlerClosed()
      emits('submited')
    })
    .catch(() => {
      ElMessage.error('分配失败！')
    })
}
/**
 * 关闭对话框处理函数
 *
 * 该函数用于关闭当前显示的对话框，通过将dialogVisible响应式变量设置为false来隐藏对话框
 */
const handlerClosed = () => {
  dialogVisible.value = false
  workOrderListData.value = []
}

/**
 * 打开对话框
 *
 * @description 设置对话框可见状态为true，用于显示对话框
 * @returns {void}
 */
const open = (val: any) => {
  dialogVisible.value = true
  workOrderListData.value = val
}

defineExpose({ open })
</script>
<style lang="scss" scoped></style>
